<?php
declare (strict_types = 1);

namespace app\api\controller\v1;

use app\api\model\v1\AdminLog;
use app\api\model\v1\TemporaryAlbum;
use app\api\model\v1\TemporaryFolder;
use app\BaseController;
use think\Request;

class Temporary extends BaseController
{


    /**
     * Notes:
     * User: liuhailong
     * DateTime: 2022/1/19 10:53
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 创建临时的文件夹
     */
    public function createTemporaryFolder(){
        $data['admin_id'] = $this->request->wx_id;
        $now_date = date('Y-m-d',time());
        $data['temporary_folder_name'] = $now_date;
        $data['username'] = $this->request->wx_user_name;
        $res = TemporaryFolder::where(['admin_id'=>$this->request->wx_id,'temporary_folder_name'=>$now_date])->find();
        if (is_null($res)){
            $res = TemporaryFolder::create($data);
            AdminLog::record('创建临时的文件夹', $data, $this->request->wx_id, $this->request->wx_user_name);
        }
        return $this->response($res->toArray());
    }


    /**
     * Notes:
     * User: liuhailong
     * DateTime: 2022/1/20 9:44
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 数据列表
     */
    public function getAlbumList(){
        $limit = $this->request->param('limit',10);
        $temporary_folder_id = $this->request->param('temporary_folder_id',0);
        $res = TemporaryFolder::find($temporary_folder_id);
        if (is_null($res)){
            return $this->response(403,'无法找到该临时存储的文件夹');
        }
        $res = TemporaryAlbum::where(['temporary_folder_id'=>$temporary_folder_id])->order('create_time desc')->paginate($limit)->each(function ($item,$key){
            $item['show'] = '';
            return $item;
        });
        return $this->response($res);
    }

    /**
     * Notes:
     * User: liuhailong
     * DateTime: 2022/1/20 11:14
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 获取图片列表
     */
    public function getPhotoArrList(){
        $ids = $this->request->param('ids');
        $new_arr = [];
        $res = TemporaryAlbum::field('id,path')->whereIn('id',$ids)->select()->toArray();
        foreach ($res as $key=>$v){
            $new_arr[$key]['url'] = $v['path'];
            $new_arr[$key]['type'] = "image";
            $new_arr[$key]['status'] = "success";
            $new_arr[$key]['thumb'] = $v['path'];
            $new_arr[$key]['size'] = '';
            $new_arr[$key]['message'] = '';
        }
        return $this->response($new_arr);
    }

    /**
     * Notes:
     * User: liuhailong
     * DateTime: 2022/3/7 13:44
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 根据用户名及文件夹日期获取
     */
    public function getPhotoAllList(){
        $date = date('Y-m-d');
        $user_id = $this->request->wx_id;
        $temporary_folder = TemporaryFolder::with(['album'=>function($query){
            $query->field('id,user_id,temporary_folder_id,path as url,create_time');
        }])->where(['admin_id'=>$user_id,'temporary_folder_name'=>$date])->find();
        if (is_null($temporary_folder)){
            return $this->response(400,'未找到文件夹');
        }
        return $this->response($temporary_folder->toArray());
    }



    /**
     * Notes:
     * User: liuhailong
     * DateTime: 2022/1/20 9:07
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 临时文件
     */
    public function saveAlbum(){
        $data = $this->request->param();
        $data['user_id'] = $this->request->wx_id;
        $data['username'] = $this->request->wx_user_name;
        $res = TemporaryFolder::find($data['temporary_folder_id']);
        if (is_null($res)){
           return $this->response(403,'无法找到该临时存储的文件夹');
        }
        $new_arr = TemporaryAlbum::create($data);
        AdminLog::record('上传文件到临时文件夹', $data, $this->request->wx_id, $this->request->wx_user_name);
        return $this->response($new_arr->toArray());
    }
}
